import axios from 'axios'
import config from './config'
import router from '@/router'
import { ElMessage } from 'element-plus'
import i18n from '@/i18n'
const { t } = i18n.global

// 配置axios
export default function request(options) {
    return new Promise((resolve, reject) => {
        // 基础配置
        const instance = axios.create({ ...config })
        // 请求拦截
        instance.interceptors.request.use(
            (config) => {
                // 配置token
                let token = localStorage.getItem("Authorization")
                if (token) {
                    config.headers.Authorization = token
                } else {
                    // 未登录，跳转登录页面
                    router.push('/login')
                }
                
                return config
            },
            (error) => {
                console.log("request:" + error);
                // 请求超时
                if (error.code === "ECONNABORTED" && error.message.indexOf("timeout") !== -1) {
                    ElMessage({message: "请求超时", type: "error", showClose: true})
                }

                return Promise.reject(error)
            }
        )

        // 响应拦截
        instance.interceptors.response.use(
            (response) => {
                return response.data
            },
            (err) => {
                // 请求失败，处理
                if (err && err.response) {
                    switch (err.response.status) {
                        case 400:
                            err.message = t('errMsg.requestError')
                            break
                        case 401:
                            err.message = t('errMsg.unauthorized')
                            break
                        case 403:
                            err.message = t('errMsg.accessDenied')
                            break
                        case 404:
                            err.message = t('errMsg.notFound') + err.response.config.url
                            break
                        case 408:
                            err.message = t('errMsg.requestTimeout')
                            break
                        case 500:
                            err.message = t('errMsg.serverError')
                            break
                        case 501:
                            err.message = t('errMsg.noService')
                            break
                        case 502:
                            err.message = t('errMsg.badGateway')
                            break
                        case 503:
                            err.message = t('errMsg.serviceUnavailable')
                            break
                        case 504:
                            err.message = t('errMsg.gatewayTimeout')
                            break
                        case 505:
                            err.message = t('errMsg.notSupport')
                            break
                        default:        
                    }
                }
                console.error(err)
                if (err.message) {
                    ElMessage({message: err.message, type: "error", showClose: true})
                }
                return Promise.reject(err)
            }
        )

        // 请求处理
        instance(options)
        .then((res) => {
            // 请求成功
            if (res.code === 200) {
                resolve(res)
            } else {
                // 未登录，跳转登录页面
                if (res.code === 401) {
                    router.push('/login')
                }
                ElMessage({message: res.msg || t('errMsg.operationFailed'), type: "error", showClose: true})

                reject(res)
            }
        })
        .catch((err) => {
            reject(err)
        })
    })
}